<script>
    (function($) {        
        $(document).ready( function() {
            $("#member_type").val($("#target_type").val());
            targetTypeHandler();

            $('a.captcha_image').click(function(e) {            
                $.post(
                    base_url + "index.php/member/captcha",
                    function(data) {                    
                        $('#captcha_word').val(data.word);
                        $('#captcha_image').empty();
                        $('#captcha_image').prepend(data.image);
                    },
                    'json');

                e.preventDefault();
            });
            
            $("#member_type").change(function(e) {
                targetTypeHandler();
            });
        });
    })(jQuery);
    
    function targetTypeHandler() {
        var member_type = $("#member_type").val();
        if (member_type == '') {
            $(".non_vendor").hide("slow");
            $(".vendor").hide("slow");
        }
        else if (member_type == 3) {
            $(".non_vendor").hide("slow");
            $(".vendor").show("slow");
        }
        else {
            $(".non_vendor").show("slow");
            $(".vendor").hide("slow");
        }        
    }
</script>
<?php
    //$this->load->model('Member_model');
    $this->db->limit(1);
    $member_id = $this->session->userdata('userid');
    $query = $this->db->get_where('psacar_member', array('id' => $member_id));
    $member = $query->row();
?>
<div id="content">
    <div class="container_medium">        
        <div class="modelGroup1">
            <h1>
<?php
                echo $member_id ? 'Member profile' : 'Member registration';
?>                
            </h1>
<?php  
        foreach($target_type->result() as $row)
        {
?>
            <input type="hidden" id="target_type" value="<?php echo $row->id; ?>" />
<?php
        }
        
        $attributes = array(
            'class' => 'email', 
            'id' => 'targets'); 
        echo form_open_multipart(base_url()."index.php/member/register/", $attributes );        
?>
        <table class="table_member">
            <tr>
                <td>
                    <span class="mandatory">Member type</span>
                </td>
                <td>
                    <select name="member_type" id="member_type" class="mandatory">
                        <option value="">Please Select</option>
<?php
                    foreach ($member_type->result() as $row)
                    {
?>
                        <option title="<?php echo $row->description; ?>" value="<?php echo $row->id; ?>">
<?php
                            echo $row->member_type;
?>
                        </option>
<?php
                    }
?>
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <strong><br>Account</strong>
                </td>
            </tr>
            <tr>
                <td>
                    <span class="mandatory">Username</span>
                </td>
                <td>
                    <input type="text" name="username" id="username" value="<?php echo $member ? $member->name : ''; ?>" class="mandatory"/> 
                </td>
            </tr>            
            <tr>
                <td valign="top">
                    <span class="mandatory">Password</span>
                    <br>6-20 characters (A-Z, a-z, 0-9)
                </td>
                <td>
                    <input type="password" name="password" id="password" style="width: 252px;" class="mandatory_password"/>                    
                </td>
            </tr>
            <tr>
                <td>
                    <span class="mandatory">Re-type password</span>
                </td>
                <td>
                    <input type="password" id="confirm_password" style="width: 252px;" class="mandatory_password"/>
                </td>
            </tr>  
            <tr class="vendor">
                <td colspan="2">
                    <strong><br>Company</strong>
                </td>
            </tr>
            <tr class="vendor">
                <td>
                    <span class="mandatory">Company name</span>
                </td>
                <td>
                    <input type="text" name="company_name" id="company_name" class="mandatory"/>
                </td>
            </tr>
            <tr class="vendor">
                <td>
                    <span class="mandatory">Company email</span>
                </td>
                <td>
                    <input type="text" name="company_email" id="company_email" value="<?php echo $member ? $member->email : ''; ?>" class="mandatory_email"/>
                </td>
            </tr>
            <tr class="vendor">
                <td>
                    Company website:
                </td>
                <td>
                    <input type="text" name="company_website" value="<?php echo $member ? $member->website : ''; ?>" />
                </td>
            </tr>
            <tr class="vendor">
                <td>
                    Company Address:
                </td>
                <td>
                    <textarea name="company_address" id="company_address" value="<?php echo $member ? $member->address : ''; ?>"></textarea>
                </td>
                <td>                    
                </td>
            </tr>
            <tr class="vendor">
                <td colspan="2">
                    <strong><br>Contact</strong>
                </td>
            </tr>
            <tr class="vendor">
                <td>
                    <span class="mandatory">Family name</span>
                </td>
                <td>
                    <input type="text" name="vendor_lastname" id="lastname" value="<?php echo $member ? $member->last_name : ''; ?>" class="mandatory"/> 
                </td>
            </tr>
            <tr class="vendor">
                <td>
                    <span class="mandatory">First name</span>
                </td>
                <td>
                    <input type="text" name="vendor_firstname" id="firstname" value="<?php echo $member ? $member->first_name : ''; ?>" class="mandatory"/> 
                </td>
            </tr>            
            <tr class="vendor">
                <td>
                    <span class="mandatory">Contact phone 1</span>
                    <br>9 characters (0-9)
                </td>
                <td>
                    <input type="text" name="contact_phone_1" id="contact_phone_1" value="<?php echo $member ? $member->contact_phone_1 : ''; ?>" class="mandatory_phone"/>
                </td>
            </tr>
            <tr class="vendor">
                <td>
                    Contact phone 2:
                </td>
                <td>
                    <input type="text" name="contact_phone_2" id="contact_phone_2" value="<?php echo $member ? $member->contact_phone_2 : ''; ?>" class="phone" />
                </td>                
            </tr>
            <tr class="vendor">
                <td>Dealer:</td>
                <td>
                    <ul>
<?php
                    foreach ($car_type->result() as $row)
                    {
?>
                        <li>
                            <input type="checkbox" name="car_type[]" value="<?php echo $row->id; ?>" /><?php echo $row->name; ?>
                        </li>
<?php
                    }
?>                    
                    </ul>
                </td>
            </tr>
            <tr class="vendor">
                <td>
                    Service:
                </td>
                <td>
                    <ul>
<?php
                    foreach ($service->result() as $row)
                    {
?>
                        <li>
                            <input type="checkbox" name="service[]" value="<?php echo $row->id; ?>" /><?php echo $row->name; ?>
                        </li>
<?php
                    }
?>                    
                    </ul>
                </td>
            </tr>
            <tr class="vendor">
                <td>
                    Product:
                </td>
                <td>
                    <ul>
<?php
                    foreach ($product->result() as $row)
                    {
?>
                        <li>
                            <input type="checkbox" name="product[]" value="<?php echo $row->id; ?>" /><?php echo $row->name; ?>
                        </li>
<?php
                    }
?>                    
                    </ul>
                </td>
            </tr>            
            <tr class="non_vendor">
                <td colspan="2">
                    <strong><br>Contact</strong>
                </td>
            </tr>            
            <tr class="non_vendor">
                <td>
                    <span class="mandatory">Family name</span>
                </td>
                <td>
                    <input type="text" name="lastname" id="lastname" value="<?php echo $member ? $member->last_name : ''; ?>" class="mandatory"/> 
                </td>
            </tr>
            <tr class="non_vendor">
                <td>
                    <span class="mandatory">First name</span>
                </td>
                <td>
                    <input type="text" name="firstname" id="firstname" value="<?php echo $member ? $member->first_name : ''; ?>" class="mandatory"/> 
                </td>
            </tr>
            <tr class="non_vendor">
                <td>
                    <span class="mandatory">Email</span>
                </td>
                <td>
                    <input type="text" name="email" id="email" value="<?php echo $member ? $member->email : ''; ?>" class="mandatory_email"/>   
                </td>
            </tr>
            <tr class="non_vendor">
                <td>
                    <span class="mandatory">Telephone</span>
                    <br>9 characters (0-9)
                </td>
                <td>
                    <input type="text" name="phone_number" id="phone_number" value="<?php echo $member ? $member->contact_phone_1 : ''; ?>" class="mandatory_phone"/>                    
                </td>
            </tr>                        

            <tr class="non_vendor">
                <td>
                    Gender:
                </td>
                <td>
                    <input type="radio" id="gender" name="gender" value="1" checked/>
                    Male
                    <input type="radio" id="gender" name="gender" value="0" >  
                    Female                   
                </td>
            </tr>
            <tr class="non_vendor">
                <td>
                    Occupation:
                </td>
                <td>
                    <select name="occupation" id="occupation">
                        <option value="0">Please Select</option>
<?php
                    foreach ($occupation->result() as $row)
                    {
?>
                        <option>
<?php
                            echo $row->occupation;
?>
                        </option>
<?php
                    }
?>
                    </select>                    
                </td>
            </tr>
            <tr class="non_vendor">
                <td>
                    Industry:
                </td>
                <td>
                    <select name="industry" id="industry">
                        <option value="0">Please Select</option>
<?php
                    foreach ($industry->result() as $row)
                    {
?>
                        <option value="<?php $row->id; ?>">
<?php
                            echo $row->industry;
?>
                        </option>
<?php
                    }
?>
                    </select>                    
                </td>
            </tr>            
            <tr class="non_vendor">
                <td>
                    How many cars or vehicles do you own?:
                </td>
                <td>
                    <select name="number_vehicle" id="number_vehicle">
                        <option value="-1">Please Select</option>
<?php
                    for ($counter = 0; $counter <= 100; $counter++)
                    {
?>
                        <option value="<?php echo $counter; ?>">
<?php
                            echo ($counter == 0 ? $counter . ' (none)' : $counter);
?>
                        </option>
<?php
                    }
?>
                    </select>                    
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <strong><br>Image verification</strong>
                </td>
            </tr>
            <tr>
                <td>                    
                    <input type='hidden' id='captcha_word' value='<?php echo $_SESSION['captchaWord']; ?>'>
                </td>
                <td>                   
                    <div id="captcha_image">
<?php 
                    echo $captcha['image'];
?>                    
                    </div>
                </td>                
            </tr>
            <tr>
                <td>
                    <span class="mandatory">Input above code</span>
                </td>
                <td>
                    <input style="width: 224px;" type="text" id="confirm_captcha" value="" class="mandatory_captcha">
                    <a class="captcha_image" href="#">
                        <img style="vertical-align: middle;" alt="Captcha" title="Request new code" src="<?php echo base_url(); ?>system/public/img/refresh.png" />
                    </a>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" value="Register" name="submit" class="register" />
                </td>
            </tr>
        </table>
<?php 
        echo form_close() 
?>
        </div>
    </div> 
    <div class="quicklink">
<?php 
        $this->load->view('/templates/quicklinks');
?>                        
    </div>    
</div>
<div class="separator"></div>